function [hypothesises]=create_region_hypothesis(hypo_amount,reg_per_hypo,seg_struct,hypo_option)

% This function creates an hypotese of image partition  into regions. each hypotese will contain 
% reg_per_hypo regions, and the function will return hypo_amount hypoteses.

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% hypo_amount - number of wanted hypothesises.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% reg_per_hypo - number of region per hypothesis.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_struct - struct of segment's features.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% hypo_option  - 'distance' - Euclid's distance calculation for hypothesises
%                                  'kmeans' - kmeans calculation for hypothesises
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% hypothesises - hypo_amount hypothesises of regions, based on  hypo_option function on segment map.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

uni_mat = [seg_struct.seg_text_mat;seg_struct.seg_loc_mat(1:2,:); seg_struct.seg_color_mat];

dist_mat = dist(uni_mat);
dist_mat = dist_mat + diag(Inf*ones(size(dist_mat,1),1),0);
hypothesises = cell(hypo_amount,reg_per_hypo);

if strcmp(hypo_option,'distance')
    for i=1:hypo_amount %create one hypothesis in each iteration
        % random up first 5 segment as the region's head
        temp_order = randperm(seg_struct.seg_count); 
        for seg = 1:reg_per_hypo % setting the 5 region's heads
            hypothesises{i,seg} = [hypothesises{i,seg} temp_order(seg)];
        end
        %for each other segments, finding the closest region's head to it.
        for seg = reg_per_hypo+1:seg_struct.seg_count 
            temp_min_dist=zeros(reg_per_hypo,1);
            for reg = 1:reg_per_hypo
                temp_min_dist(reg) = median(dist_mat(temp_order(seg),hypothesises{i,reg})); % getting dist
            end 
            [min_dist,min_reg]=min(temp_min_dist);
            % setting the segment in region by the closest region's head to it
            hypothesises{i, min_reg} = [hypothesises{i, min_reg} temp_order(seg)];
        end
    end 
end 

if strcmp(hypo_option,'kmeans')
    for i=1:hypo_amount %create one hypothesis in each iteration
        % partition the segments with k-means algorithm into reg_per_hypo regions
        partition = kmeans(uni_mat', reg_per_hypo);
        for reg = 1:reg_per_hypo
            hypothesises{i, reg} = find(partition==reg);
        end 
    end 
end 
